//
//  QuestionChooseView.swift
//  SwiftDemo
//
//  Created by DYD on 2018/12/5.
//  Copyright © 2018 DYD. All rights reserved.
//

import UIKit
import SDVersion
import RxSwift
import RxCocoa

let screenWidth = UIScreen.main.bounds.size.width
let screenHeight = UIScreen.main.bounds.size.height

class QuestionChooseView: UITableViewCell {
    
    lazy var dataSource = QuestionModel()

    lazy var layout: UICollectionViewFlowLayout = {
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .vertical
        layout.minimumLineSpacing = 0.0
        layout.minimumInteritemSpacing = 0.0
        return layout
    }()
    
    lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 14)
        label.numberOfLines = 0
        return label
    }()
    
    lazy var collectionView: UICollectionView = {
        let view = UICollectionView(frame: CGRect.zero, collectionViewLayout: self.layout)
        view.dataSource = self
        view.delegate = self
        view.backgroundColor = CDColor.grayBackgroundColorA
        view.register(QuestionItemCell.self, forCellWithReuseIdentifier: "QuestionItemCell")
        return view
    }()
    
    lazy var textField: UITextField = {
        let view = UITextField(frame: CGRect.zero)
        view.backgroundColor = .white
        view.delegate = self
        view.clipsToBounds = true
        view.layer.cornerRadius = 4.0
        view.layer.masksToBounds = true
        return view
    }()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = CDColor.grayBackgroundColorA
        addSubview(titleLabel)
        addSubview(collectionView)
        addSubview(textField)
        clipsToBounds = true
        
        titleLabel.snp.remakeConstraints { (make) in
            make.left.equalTo(10)
            make.right.equalTo(-10)
            make.centerY.equalTo(self.snp.top).offset(15.0)
        }
        
        collectionView.snp.remakeConstraints { (make) in
            make.top.equalTo(30.0)
            make.left.right.equalTo(0)
            make.bottom.equalTo(textField.snp.top).offset(-5)
        }
        
        textField.snp.remakeConstraints { (make) in
            make.left.equalTo(10)
            make.right.equalTo(-10)
            make.height.equalTo(30.0)
            make.bottom.equalTo(-5)
        }
    }
    
    func config(_ model: QuestionModel) {
        self.frame = CGRect(x: self.frame.origin.x, y: self.frame.origin.y, width: screenWidth, height: model.viewHeight)
        collectionView.frame = CGRect(x: collectionView.frame.origin.x, y: collectionView.frame.origin.y, width: screenWidth, height: model.viewHeight - 20.0)
        dataSource = model
        titleLabel.text = model.questionTitle
        textField.isHidden = !model.canEnterSupply
        textField.placeholder = model.supplyTip
        textField.text = model.enterDetail
        _ = textField.rx.text.subscribe(onNext: {[weak self] (text) in
            if let strongSelf = self {
                strongSelf.dataSource.enterDetail = text ?? ""
            }
        }, onError: nil, onCompleted: nil, onDisposed: nil)
        
        let textFieldHeight = model.canEnterSupply ? 30.0 : 0.0
        textField.snp.updateConstraints { (make) in
            make.height.equalTo(textFieldHeight)
        }
        self.layoutIfNeeded()
        collectionView.reloadData()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension QuestionChooseView: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UITextFieldDelegate {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dataSource.items.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "QuestionItemCell", for: indexPath) as! QuestionItemCell
        cell.config(dataSource.items[indexPath.item])
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        if (indexPath.item >= dataSource.items.count) {
            return
        }
        
        if dataSource.multiChoose {
            if dataSource.selectedIndex.contains(indexPath.item){
                dataSource.selectedIndex = dataSource.selectedIndex.filter { (obj) -> Bool in
                    obj != indexPath.item
                }
            } else {
                dataSource.selectedIndex.append(indexPath.item)
            }
        } else {
            dataSource.selectedIndex = [indexPath.item]
        }
        
        
        collectionView.reloadData()
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return dataSource.itemSize
    }
    
    
    
}
